package com.example.com.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.com.common.R;
import com.example.com.common.widget.PlaceHolderView;

/**
 * 占位布局
 * 实现了一个图片显示和一个文本描述
 * 用于显示空界面及错误界面等
 * Created by ChenYiYao on 2018/2/27.
 */

public class EmptyView extends LinearLayout implements PlaceHolderView {
    private ImageView mImageView;
    private TextView mTextView;
    private ProgressBar mProgressBar;
    private Button mBtnEmpty;

    private int[] mDrawableIds = new int[]{0, 0};
    private int[] mTextIds = new int[]{0, 0, 0};

    private View[] mBindViews;
    //是否显示刷新按钮标记
    private boolean mIsShowLoadBtn = false;

    public EmptyView(Context context) {
        this(context, null);
    }

    public EmptyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmptyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        inflate(getContext(), R.layout.lay_empty, this);
        mImageView = findViewById(R.id.im_empty);
        mTextView = findViewById(R.id.tv_empty);
        mProgressBar = findViewById(R.id.progressBar);
        mBtnEmpty = findViewById(R.id.btn_empty);

        //加载自定义属性
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmptyView, defStyleAttr, 0);
        mDrawableIds[0] = a.getInt(R.styleable.EmptyView_comEmptyDrawable, R.drawable.ic_empty);
        mDrawableIds[1] = a.getInt(R.styleable.EmptyView_comErrorDrawable, R.drawable.ic_empty);
        mTextIds[0] = a.getInt(R.styleable.EmptyView_comEmptyText, R.string.prompt_empty);
        mTextIds[1] = a.getInt(R.styleable.EmptyView_comErrorText, R.string.network_error);
        mTextIds[2] = a.getInt(R.styleable.EmptyView_comLoadingText, R.string.prompt_loading);

        a.recycle();
    }

    /**
     * 绑定一系列数据显示的布局
     * 当前布局隐藏时（有数据时）自动显示绑定的数据布局
     * 而当数据加载时，自动显示Loading，并隐藏数据布局
     *
     * @param views 数据显示的布局
     */
    public void bind(View... views) {
        this.mBindViews = views;
    }

    private void changeBindViewBility(int visible) {
        final View[] views = mBindViews;
        if (views == null || views.length == 0)
            return;

        for (View view : views) {
            view.setVisibility(visible);
        }
    }

    @Override
    public void triggerEmpty() {
        mImageView.setImageResource(mDrawableIds[0]);
        mTextView.setText(mTextIds[0]);
        mImageView.setVisibility(VISIBLE);
        mTextView.setVisibility(VISIBLE);
        mProgressBar.setVisibility(GONE);
        mBtnEmpty.setVisibility(mIsShowLoadBtn ? VISIBLE : GONE);
        setVisibility(VISIBLE);
        changeBindViewBility(GONE);

    }

    @Override
    public void triggerNetError() {
        mImageView.setImageResource(mDrawableIds[1]);
        mTextView.setText(mTextIds[1]);
        mImageView.setVisibility(VISIBLE);
        mTextView.setVisibility(VISIBLE);
        mProgressBar.setVisibility(GONE);
        mBtnEmpty.setVisibility(mIsShowLoadBtn ? VISIBLE : GONE);
        setVisibility(VISIBLE);
        changeBindViewBility(GONE);
    }

    @Override
    public void triggerError(String msg) {
        mTextView.setText(msg);
        mImageView.setVisibility(VISIBLE);
        mTextView.setVisibility(VISIBLE);
        mProgressBar.setVisibility(GONE);
        mBtnEmpty.setVisibility(mIsShowLoadBtn ? VISIBLE : GONE);
        setVisibility(VISIBLE);
        changeBindViewBility(GONE);
    }


    @Override
    public void setIcon(int draRes) {

    }

    @Override
    public void triggerLoading() {
        mImageView.setVisibility(GONE);
        mTextView.setText(mTextIds[2]);
        mProgressBar.setVisibility(VISIBLE);
        mBtnEmpty.setVisibility(GONE);
        setVisibility(VISIBLE);
        changeBindViewBility(GONE);
    }

    @Override
    public void triggerOk() {
        setVisibility(GONE);
        changeBindViewBility(VISIBLE);

    }

    /**
     * @param isOk 是否加载成功数据，隐藏空界面
     */
    @Override
    public void triggerOkOrEmpty(boolean isOk) {
        if (isOk) {
            triggerOk();
        } else {
            triggerEmpty();
        }
    }

    /**
     *
     * @param flag ture可见 false不可见
     */
    @Override
    public void setRefreshBtnVisible(boolean flag) {
        mIsShowLoadBtn = flag;
        mBtnEmpty.setVisibility(mIsShowLoadBtn ? VISIBLE : GONE);
    }

    @Override
    public void addRefreshBtnEvent(OnClickListener listener) {
        mBtnEmpty.setOnClickListener(listener);
        mIsShowLoadBtn = true;
        if (mProgressBar.getVisibility() != View.VISIBLE) {
            //若在加载中，则不显示
            mBtnEmpty.setVisibility(VISIBLE);
        }
    }
}
